package edu.scut.luluteam.contextlib;

import android.app.Application;
import android.provider.Settings;
import android.util.Log;

import edu.scut.luluteam.contextlib.Tools.JumpHelper;
import edu.scut.luluteam.contextlib.collection.MsgLifecycleCallback;
import edu.scut.luluteam.contextlib.constant.AppHolder;
import edu.scut.luluteam.contextlib.utils.PermissionUtil;
import edu.scut.luluteam.contextlib.utils.ThreadUtil;

/**
 * @author Guan
 * @date Created on 2018/4/2
 */
@Deprecated
public class ContextClient {

    private static ContextClient instance;
    private static final String TAG = "ContextClient";

    //===================================

    /**
     * SDK 的入口
     *
     * @param context
     */
    public static ContextClient init(Application context, String APP_ID) {
        if (instance == null) {
            synchronized (ContextClient.class) {
                if (instance == null) {
                    instance = new ContextClient(context, APP_ID);
                }
            }
        }
        return instance;
    }

    @Deprecated
    public static void requestContext() {
        if (instance == null) {
            Log.e(TAG, "操作失败：Context Client 未初始化");
            return;
        }
//        JumpHelper.gotoLocationService();
    }


    /**
     * U-I-R-C
     * <p>
     * userId：用deviceId代替
     *
     * @param itemId
     * @param rating
     * @param duration
     */
    public static void rating(String itemId, int rating, long duration, int wordCount) {
        if (instance == null) {
            Log.e(TAG, "操作失败：Context Client 未初始化");
            return;
        }
        JumpHelper.gotoLocationService(itemId, rating, duration, wordCount);
    }

    //================================

    private ContextClient(Application context, String APP_ID) {
        AppHolder.appContext = context;
        //获取AndroidId，用于标识设备
        AppHolder.androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        //
        AppHolder.APP_ID = APP_ID;
        //启动
        if (ThreadUtil.checkRunOnMainThread()
                && PermissionUtil.checkPermission(AppHolder.appContext, AppHolder.PERMISSIONS)) {
            Log.i(TAG, "Context Client 启动成功");
        } else {
            Log.e(TAG, "Context Client 启动失败");
        }
        start();
    }


    //============================


    /**
     * 做一些其他信息的启动
     */
    private void start() {

        //启动监听activity的生命周期
        ((Application) AppHolder.appContext).registerActivityLifecycleCallbacks(new MsgLifecycleCallback());

    }


}
